package sort;

/**
 * 冒泡排序的实现。时间复杂度O(N^2),空间复杂度O(1)
 *
 * @author sjm
 * @return
 * @creed: Wellerman
 * @date 2022/3/3 13:12
 */
public class BubblingSort {


    public static void Bubbling(int[] arrs) {
        //遍历
        for (int i = arrs.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                //判断
                if (arrs[j] >= arrs[i]) {
                    //交换
                    swap(arrs, i, j);
                }
            }
        }
    }

    private static void swap(int[] arrs, int i, int j) {
//        int temp = arrs[i];
//        arrs[i] = arrs[j];
//        arrs[j] = temp;
        //异或运算，同0非1，满足交换律，结合律（必须保证两块内存是独立的）
        arrs[i] = arrs[i] ^ arrs[j];
        arrs[j] = arrs[i] ^ arrs[j];
        arrs[i] = arrs[i] ^ arrs[j];
    }

    public static void main(String[] args) {
        int[] arrs = {8, 5, 23, 1, 5};
        Bubbling(arrs);
        for (int i = 0; i < arrs.length; i++) {
            System.out.println(arrs[i]);
        }
    }
}
